package com.ai.android.testmenus.drawing3;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.media.Image;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.app.AlertDialog;
import android.widget.Toast;

import java.util.UUID;


public class Main extends Activity implements View.OnClickListener{
    private Drawing drawing;
    private ImageButton currPaint, drawButton, eraseButton, newButton, saveButton;
    private float smallBrush, mediumBrush, largeBrush;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        smallBrush = getResources().getInteger(R.integer.small_size);
        mediumBrush = getResources().getInteger(R.integer.medium_size);
        largeBrush = getResources().getInteger(R.integer.large_size);
        drawing = (Drawing)findViewById(R.id.drawing);
        LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
        currPaint = (ImageButton)paintLayout.getChildAt(0);
        currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
        drawing.setBrushSize(mediumBrush);
        drawButton = (ImageButton) findViewById(R.id.draw_btn);
        drawButton.setOnClickListener(this);
        eraseButton = (ImageButton) findViewById(R.id.erase_btn);
        eraseButton.setOnClickListener(this);
        newButton = (ImageButton)findViewById(R.id.new_btn);
        newButton.setOnClickListener(this);
        saveButton = (ImageButton)findViewById(R.id.save_btn);
        saveButton.setOnClickListener(this);
    }


    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.draw, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }*/
    @Override
    public void onClick(View view){

        //witch(view.getId()){
            //case R.id.draw_btn:
        if(view.getId()==R.id.draw_btn){
                final Dialog brushDialog = new Dialog(this);
                brushDialog.setTitle("Brush Size");
                brushDialog.setContentView(R.layout.brush_chooser);
                ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
                smallBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        drawing.setBrushSize((smallBrush));
                        drawing.setLastBrushSize(smallBrush);
                        drawing.setErase(false);
                        brushDialog.dismiss();
                    }
                });
                ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
                mediumBtn.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        drawing.setBrushSize(mediumBrush);
                        drawing.setLastBrushSize(mediumBrush);
                        drawing.setErase(false);
                        brushDialog.dismiss();
                    }
                });

                ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
                largeBtn.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        drawing.setBrushSize(largeBrush);
                        drawing.setLastBrushSize(largeBrush);
                        drawing.setErase(false);
                        brushDialog.dismiss();
                    }
                });
                brushDialog.show();
        }
            //case R.id.erase_btn:
        else if(view.getId()==R.id.erase_btn){
                final Dialog eraseDialog = new Dialog(this);
                eraseDialog.setTitle("Eraser size:");
                eraseDialog.setContentView(R.layout.brush_chooser);
                ImageButton smallBtn= (ImageButton)eraseDialog.findViewById(R.id.small_brush);
                smallBtn.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        drawing.setErase(true);
                        drawing.setBrushSize(smallBrush);
                        eraseDialog.dismiss();
                    }
                });
                ImageButton mediumBtn = (ImageButton)eraseDialog.findViewById(R.id.medium_brush);
                mediumBtn.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        drawing.setErase(true);
                        drawing.setBrushSize(mediumBrush);
                        eraseDialog.dismiss();
                    }
                });
                ImageButton largeBtn = (ImageButton)eraseDialog.findViewById(R.id.large_brush);
                largeBtn.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        drawing.setErase(true);
                        drawing.setBrushSize(largeBrush);
                        eraseDialog.dismiss();
                    }
                });
                eraseDialog.show();
        }
            //case R.id.new_btn:
        else if(view.getId()==R.id.new_btn){
                AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
                newDialog.setTitle("New drawing");
                newDialog.setMessage("Start new drawing (you will lose the current drawing)?");
                newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        drawing.starNewDrawing();
                        dialog.dismiss();
                    }
                });
                newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which){
                        dialog.cancel();
                    }
                });
                newDialog.show();
        }
            //case R.id.save_btn:
        else if(view.getId()==R.id.save_btn){
                    AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
                    saveDialog.setTitle("Save drawing");
                    saveDialog.setMessage("Save drawing to device Gallery?");
                    saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which){
                            drawing.setDrawingCacheEnabled(true);
                            String imgSaved = MediaStore.Images.Media.insertImage(getContentResolver(), drawing.getDrawingCache(), UUID.randomUUID().toString()+".png", "drawing");
                            if(imgSaved!=null){
                                Toast savedToast = Toast.makeText(getApplicationContext(),
                                        "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                                savedToast.show();
                            }
                            else{
                                Toast unsavedToast = Toast.makeText(getApplicationContext(),
                                        "Oops! Image could not be saved.", Toast.LENGTH_SHORT);
                                unsavedToast.show();
                            }
                            drawing.destroyDrawingCache();
                        }
                    });
                    saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int which){
                            dialog.cancel();
                        }
                    });
                    saveDialog.show();

            //default:
        }

    }

    public void paintClicked(View view){
        if(view!=currPaint){
            drawing.setErase(false);
            drawing.setBrushSize(drawing.getLastBrushSize());
            ImageButton imgView = (ImageButton)view;
            String color = view.getTag().toString();
            drawing.setColor(color);
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
            currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
            currPaint=(ImageButton)view;
        }
    }
}
